package come.demo.widgets.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import come.demo.widgets.BasePageAdapter;
import come.demo.widgets.BaseRecyclerAdapter;
import come.demo.widgets.R;
import tzy.refreshlayout.CustomRefreshLayout;
import tzy.scrollview.CustomNestedScrollView;

public class CustomNestedActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private CustomNestedScrollView mCustomNestedScrollView;
    private RecyclerView mHeaderRecyclerView;
    private SwipeRefreshLayout mRefreshLayout;
//    private CustomRefreshLayout mCustomRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_nested);
        mRefreshLayout = findViewById(R.id.custom_refresh_layout);
        mRefreshLayout.setOnRefreshListener(this);
        mHeaderRecyclerView = findViewById(R.id.recyclerview);
        mHeaderRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        HeaderAdapter adapter = new HeaderAdapter();
        mHeaderRecyclerView.setAdapter(adapter);


        mViewPager = findViewById(R.id.viewpager);
        final MyListFragment[] fragments = new MyListFragment[3];
        for (int i = 0; i < fragments.length; ++i) {
            fragments[i] = new MyListFragment();
        }
        mViewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), fragments));
        mTabLayout = findViewById(R.id.tablayout);
        mTabLayout.setupWithViewPager(mViewPager);
        mCustomNestedScrollView = findViewById(R.id.custom_nested);
        mCustomNestedScrollView.setOnScrollChangeListener2(new CustomNestedScrollView.OnScrollChangeListener2() {
            @Override
            public void onScrollChange(CustomNestedScrollView v, int scrollY, int oldScrollY, int topViewAt, View topView) {
                Log.i("@@", "@@@@@@@@@@:" + topViewAt + "," + topView.getClass().getSimpleName());
            }
        });


    }

    @Override
    public void onRefresh() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mRefreshLayout.setRefreshing(false);
            }
        }, 1000);
    }

    private static class HeaderAdapter extends RecyclerView.Adapter<CustomNestedActivity.HeaderAdapter.ViewHolder> {
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_custom_refresh_header, null));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.imageView.setImageResource(R.mipmap.ic_launcher_round);
            holder.textView.setText(String.valueOf(position));
        }

        @Override
        public int getItemCount() {
            return 50;
        }

        static class ViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;
            TextView textView;

            public ViewHolder(View itemView) {
                super(itemView);
                imageView = itemView.findViewById(R.id.item_custom_image);
                textView = itemView.findViewById(R.id.item_custom_text);
            }
        }
    }

    private static class ViewPagerAdapter extends FragmentPagerAdapter {

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return String.valueOf(position);
        }

        MyListFragment[] listFragments;

        public ViewPagerAdapter(FragmentManager fm, MyListFragment[] listFragments) {
            super(fm);
            this.listFragments = listFragments;
        }

        @Override
        public Fragment getItem(int position) {
            return listFragments[position];
        }

        @Override
        public int getCount() {
            return listFragments.length;
        }
    }


    public static class MyListFragment extends Fragment implements CustomRefreshLayout.OnRefreshLoadListener {
        private RecyclerView mRecyclerView;
//        private CoustomSwipeRefreshLayout mySwipeRefreshLayout;

        private CustomRefreshLayout customRefreshLayout;
        RefreshActivity2.SimpleAdapter mAdapter;
        private final Handler mHandler = new Handler();

        @Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_refresh2, container, false);
        }


        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mRecyclerView = view.findViewById(R.id.recyclerview);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
            mRecyclerView.setAdapter(mAdapter = new RefreshActivity2.SimpleAdapter(null));
//
////            mRecyclerView.setEmptyView(view.findViewById(R.id.empty));
//
            customRefreshLayout = view.findViewById(R.id.swiperefreshlayout);
            customRefreshLayout.setRefreshEnabled(false);
            customRefreshLayout.setOnRefreshLoadListener(this);


            final String[] strings = new String[50];
            for (int i = 0; i < strings.length; ++i) {
                strings[i] = String.valueOf(i);
            }
            mAdapter.refreshDataArrayWithHasNextPage(strings);

            customRefreshLayout.setFinished(false);

        }

        @Override
        public void onDestroyView() {
            mHandler.removeCallbacksAndMessages(null);
            super.onDestroyView();
        }


        @Override
        public void onRefresh(CustomRefreshLayout view) {

        }

        @Override
        public void onLoading(CustomRefreshLayout view) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
//                    customRefreshLayout.setLoadEnabled(false);

                    final String[] strings = new String[20];
                    for (int i = 0; i < strings.length; ++i) {
                        strings[i] = String.valueOf(mAdapter.getItemCount() + i);
                    }
                    mAdapter.addDataArrayWithHasNextPage(strings);

                    customRefreshLayout.setFinished(false);
                }
            }, 3000);
        }
    }

    public static class SimpleAdapter extends BasePageAdapter<String, BaseRecyclerAdapter.BaseViewHolder> {

        public SimpleAdapter(List<String> data) {
            super(data);
        }


        @Override
        public BaseRecyclerAdapter.BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new BaseViewHolder(new TextView(parent.getContext()));
        }

        @Override
        public void onBindViewHolder(BaseRecyclerAdapter.BaseViewHolder holder, int position) {
            ((TextView) (holder.itemView)).setText(getItem(position));

        }

    }


    public static void gotoActivity(Context context) {
        Intent intent = new Intent(context, CustomNestedActivity.class);
        context.startActivity(intent);
    }
}
